package aryn.mapEditor;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.UIManager;

import aryn.framework.display.MyPanel;

public class Menu extends MyPanel {
	private static final long serialVersionUID = 1L;
	
	public enum choose {APPEND, REPLACE, EXCLUSIVE, CANCEL, UNDEFINED}
	private choose answer;
	private BufferedImage[][] tileset;
	private ArrayList<BufferedImage[][]> tilesets;
	private int currentIndex = 0;
	private int[][] tilesetInd = new int[8][8];
	private ArrayList<int[][]> tilesetsInd;
	
	public Menu(String name, int x, int y, int width, int height) {
		super(name, x, y, width, height);
		addMouseListener();
		addMouseMotionListener();
		Loader loader = Loader.getInstance();
		addImage(loader.getMenuImg(), 0, 0);
		tileset = new BufferedImage[8][8];
		tilesets = new ArrayList<BufferedImage[][]>();
		tilesetsInd = new ArrayList<int[][]>();
		int btwW = 90;
		int btnH = 30;
		for(int k = 0; k < 8; k++)
			for(int i = 0; i < 8; i++)
				tilesetInd[i][k] = -1;
		
		addButton("save map", "saves the map in *.map extension", Config.getBtnSaveMap(), btwW, btnH, -1,
				new ActionListener() {public void actionPerformed(ActionEvent e) {
						saveMap();	}});
		addButton("load map", "loads a *.map", Config.getBtnLoadMap(), btwW, btnH, -1,
				new ActionListener() {public void actionPerformed(ActionEvent e) {
						loadMap();	}});
		addButton("cancel", "cancels selected tiles", Config.getBtnCancel(), btwW, btnH, KeyEvent.VK_CANCEL,
				new ActionListener() {public void actionPerformed(ActionEvent e) {
						cancel();	}});
		addButton("fill", "fills selected tiles with the current one.", Config.getBtnFill(),
				btwW, btnH, -1,	new ActionListener() {public void actionPerformed(ActionEvent e) {
						fill();		}});
		addButton("load set", "A popup makes you specify how you want to load the tileset.",
				Config.getBtnLoadSet(), btwW, btnH, -1, new ActionListener() {public void actionPerformed(ActionEvent e) {
						loadSet();	}});
		addButton("delete set", "A popup makes you specify how you want to delete the tileset.",
				Config.getBtnDelSet(), btwW, btnH, -1, new ActionListener() {public void actionPerformed(ActionEvent e) {
						deleteSet();}});
		addButton("<<", "shows preview tileset", Config.getBtnLeftArrow(), 50, 20, -1,
				new ActionListener() {public void actionPerformed(ActionEvent e) {
						leftArr();	}});
		addButton(">>", "shows next tileset", Config.getBtnRightArrow(), 50, 20, -1,
				new ActionListener() {public void actionPerformed(ActionEvent e) {
						rightArr();	}});
	}
	
	public void postRender(Graphics g) {
//		if(mouse.isClicked() && Util.mouseBound(mouse.getMouseX(), mouse.getMouseY(),
//				45, 418, 309, 649))

		if(answer == choose.EXCLUSIVE) {
			for(int k = 0; k < currentIndex+1; k++)
				for(int i = 0; i < 8; i++)
					for(int j = 0; j < 8; j++)
						if(tilesetInd[j][i] != -1) deleteImage(tilesetsInd.get(k)[j][i]); 

			tilesets = new ArrayList<BufferedImage[][]>();
			tilesets.add(tileset);
			currentIndex = 0;
			
			for(int i = 0; i < 8; i++)
				for(int j = 0; j < 8; j++)
					tilesetInd[i][j] = addImage(tileset[i][j], 46 + i*33, 419 + j*33);
			tilesetsInd = new ArrayList<int[][]>();
			tilesetsInd.add(tilesetInd);
			answer = choose.UNDEFINED;
		}
		if(answer == choose.APPEND) {
			tilesets.add(tileset);
			tilesetsInd.add(tilesetInd);
			currentIndex++;
			for(int i = 0; i < 8; i++)
				for(int j = 0; j < 8; j++)
					changeImage(tilesetInd[i][j], tileset[i][j]);
			answer = choose.UNDEFINED;	
		}
		if(answer == choose.REPLACE) {
			tilesets.set(currentIndex, tileset);
			tilesetsInd.set(currentIndex, tilesetInd);
			for(int i = 0; i < 8; i++)
				for(int j = 0; j < 8; j++)
					changeImage(tilesetInd[i][j], tileset[i][j]);
			answer = choose.UNDEFINED;
		}
		repaint(0,0,1,1);
	}
	
	private void saveMap() {
		
	}
	private void loadMap() {
		
	}
	private void fill() {
		
	}
	private void cancel() {
		
	}
	private void loadSet() {
		try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {e.printStackTrace();} 
		JFileChooser chooser = new JFileChooser(new File("."));
		int returnVal = chooser.showOpenDialog(this);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	       tileset = Loader.getInstance().loadTileset((chooser.getSelectedFile().getAbsolutePath()));
	       if(tilesetInd[0][0] == -1) {
	    	   for(int i = 0; i < 8; i++)
					for(int j = 0; j < 8; j++)
						tilesetInd[i][j] = addImage(tileset[i][j], 46 + i*33, 419 + j*33);
				tilesetsInd = new ArrayList<int[][]>();
				tilesetsInd.add(tilesetInd);
				tilesets.add(tileset);
			} else new MyPopup(this, 500, 300);
	    }
	}
	private void deleteSet() {
		
	}
	private void leftArr() {
		if (currentIndex > 0) {
			currentIndex--;
			for(int i = 0; i < 8; i++)
				for(int j = 0; j < 8; j++)
					changeImage(tilesetsInd.get(currentIndex)[i][j], tilesets.get(currentIndex)[i][j]);
		}
	}
	private void rightArr() {
		if (currentIndex < tilesetsInd.size()-1) {
			currentIndex++;
			for(int i = 0; i < 8; i++)
				for(int j = 0; j < 8; j++)
					changeImage(tilesetsInd.get(currentIndex)[i][j], tilesets.get(currentIndex)[i][j]);
		}
	}
	private void addButton(String text, String tooltip, Point position, int width, int height,
			int shortcutKey, ActionListener listener) {
		JButton arrL = new JButton();
		arrL.setToolTipText(tooltip);
		arrL.setText(text);
		arrL.setSize(width, height);
		arrL.setLocation(position);
		if (shortcutKey > 0) arrL.setMnemonic(shortcutKey);
		arrL.addActionListener(listener);
		add(arrL);
	}

	public void setAnswer(choose answer) {
		this.answer = answer;
	}
}